package com.ds.robot.assistant.controller;

import com.google.common.cache.LoadingCache;
import com.ds.robot.assistant.common.R;
import com.ds.robot.assistant.job.QxUserJob;
import com.ds.robot.assistant.service.QxUserApiService;
import com.ds.robot.assistant.service.QxUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 作用：
 *
 * @author WeiShaoying
 * 
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class QxUserApiController {

    @Autowired
    private QxUserService qxUserService;

    @Autowired
    private QxUserApiService qxUserApiService;

    // @Resource
    // private RedisTemplate redisTemplate;

    @Autowired
    private QxUserJob qxUserJob;

    @Resource
    private LoadingCache<String, String> qxUserCache;

    @Value("${qx.access_token_key}")
    private String accessTokenKey;

    @GetMapping("/getCache")
    public Map<String, String> getAllCacheContent() {
        try {
            return qxUserCache.asMap();
        } catch (Exception e) {
            throw new RuntimeException("查询用户缓存异常", e);
        }
    }
    // @Autowired
    // private IRequestTokenService requestTokenService;

    // @RequestMapping("/first")
    // public R init() {
    //     qxUserJob.firstInsert();
    //     return R.ok("success");
    // }

    @RequestMapping("/getAccessToken")
    public R getAccessToken() {
        // String accessToken = (String) redisTemplate.opsForValue().get(accessTokenKey);
        // String accessToken = requestTokenService.getAccessToken();
        String accessToken = qxUserApiService.queryAccessToken();
        return R.ok().put("data", accessToken);
    }

}
